package androidgames.networkgames.screens;

import java.io.IOException;
import java.util.ArrayList;

import android.graphics.Color;
import android.graphics.Paint.Style;
import android.os.Environment;
import android.util.Log;
import androidgames.configs.AppConfig;
import androidgames.configs.UIConfig;
import androidgames.data.CheckData;
import androidgames.data.gameFile;
import androidgames.data.ReadXML;
import androidgames.networkgames.screens.components.DrawableComponent;
import androidgames.networkgames.screens.components.DrawableComponent.CEvent;
import androidgames.networkgames.screens.components.DrawableComponent.IEventListener;
import androidgames.networkgames.screens.components.BackgroundAnimation;
import androidgames.networkgames.screens.components.MenuItem;
import androidgames.networkgames.screens.components.NativeMenu;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics.PixmapFormat;
import com.badlogic.androidgames.framework.Input.TouchEvent;
import com.badlogic.androidgames.framework.Pixmap;
import com.badlogic.androidgames.framework.Sound;

public class ScreenTest extends NetworkScreen implements IEventListener,
		androidgames.networkgames.screens.components.MenuItem.IEventListener {
	MenuItem item1, item2, item3, item4;
	private Sound start = null;
	private BackgroundAnimation backgroundAnimetion;
	private int playtime = 0;
	private ArrayList<gameFile> listFile;
	private CheckData check;

	public ScreenTest(Game game) {
		super(game);
		start = game.getAudio().newSound("Sounds/startup.mp3");

		NativeMenu nativeMenu = new NativeMenu(this, 5, 100);
		item1 = new MenuItem("Stop BG", null);
		item2 = new MenuItem("Start BG", null);
		item3 = new MenuItem("On", null);
		item4 = new MenuItem("OFFFFFFFF", null);

		item1.addTouchListener(this);
		item2.addTouchListener(this);
		item3.addTouchListener(this);
		item4.addTouchListener(this);

		nativeMenu.addItem(item1);
		nativeMenu.addItem(item2);
		nativeMenu.addItem(item3);
		nativeMenu.addItem(item4);
		addComponent(nativeMenu);

		ArrayList<Pixmap> listBG = new ArrayList<Pixmap>();
		listBG.add(g.newPixmap(UIConfig.LOGIN_BACKGROUND, PixmapFormat.RGB565));
		listBG.add(g.newPixmap("UI/background/black_cat.jpg",
				PixmapFormat.RGB565));
		backgroundAnimetion = new BackgroundAnimation(listBG);
		// nativeMenu.setItemWidth(30);
		background = backgroundAnimetion.getBackground();

//		ReadXML readXML = new ReadXML();
//		listFile = new ArrayList<gameFile>();
//		listFile = readXML.read(AppConfig.FILE_XML_SERVER_PATH);
//		Log.d("list", "" + listFile.size());
//
//		check = new CheckData();

	}

	@Override
	public void onDataReceive(byte[] data) throws IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public void handlePacket(byte[] data) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void update(float deltaTime) {
		super.update(deltaTime);
	}

	@Override
	public void present(float deltaTime) {
		if (playtime < 10) {
			start.play(1);
			playtime++;
		} else if (playtime >= 10) {
			start.dispose();
		}
		super.present(deltaTime);
		background = backgroundAnimetion.getBackground();

		/**
		 * check file if exists or not. False if not exists
		 */
//		try {
//			for (int i = 0; i < listFile.size(); i++) {
//				Log.d("Check file exists: ",
//						listFile.get(i).name
//								+ " "
//								+ check.checkFileMissing(Environment
//										.getExternalStorageDirectory()
//										+ "/download/" + listFile.get(i).name));
//
//				if (check.checkFileMissing(Environment
//						.getExternalStorageDirectory()
//						+ "/download/"
//						+ listFile.get(i).name) == false) {
//					check.downloadFile(listFile.get(i).downloadPath,
//							listFile.get(i).filePath);
//				}
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//			Log.d("Ex: ", e.toString());
//		}

	}

	@Override
	public void onTouch(DrawableComponent view, CEvent event) {
		// TODO Auto-generated method stub
	}

	@Override
	public void addComponent(DrawableComponent component) {
		// TODO Auto-generated method stub
		component.addTouchListener(this);
		componentList.add(component);
	}

	@Override
	public void onTouch(MenuItem menuItem, TouchEvent touchevents) {
		if (menuItem == item1) {
			backgroundAnimetion.stopBG();
		}
		if (menuItem == item2) {
			backgroundAnimetion.startBG();
		}
	}

}
